/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "entrance.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
//uint8_t sum_check = 0;
//uint8_t add_check = 0;
//#define u8 uint8_t 

//uint8_t Data_check(uint8_t *data, uint8_t length)
//{
//	
//  for(uint16_t s = 0; s < length; s++)
//	{
//		sum_check += data[s];
//		add_check += sum_check;
//	}
//	if((sum_check =data[length])&&(add_check=data[length+1]))
//	return 1;
//	else return 0;
//}

//typedef struct {
//uint8_t Loca_x;
//uint8_t Loca_y;
//uint8_t animal_type;
//uint8_t animal_num;

//}animal_str;

//animal_str animal;
//#define DT_RX_BUFNUM 100

//static void AnoDTDataAnl(u8 *data,u8 len)
//{
//	u8 check_sum1 = 0,check_sum2 = 0;
//	if(*(data+3) != (len-6))	//�ж����ݳ����Ƿ���ȷ
//	{
//		return;
//	}
//  for(u8 i=0; i<len-2; i++)
//	{
//        check_sum1 += *(data+i);
//		check_sum2 += check_sum1;
//	}
//	if((check_sum1 != *(data+len-2)) || (check_sum2 != *(data+len-1)))	//�ж�sumУ��
//	{
//    return;
//	}
//	//=============================================================================
//	if(*(data+2)==0X30) //��������
//	{
//		animal.Loca_x=*(data+4);
//		animal.Loca_y=*(data+5);
//		animal.animal_type=*(data+6);
//		animal.animal_num=*(data+7);

//	}	
//}
////===========================================================
////���ݲ�ͬͨ�ŷ�ʽ������������ݻ�����
////===========================================================
//typedef struct
//{
//	u8 DT_RxBuffer[DT_RX_BUFNUM];
//	u8 _data_len;
//	u8 _data_cnt;
//	u8 rxstate;
//}_dt_rx_anl_st;


//void AnoDTRxOneByte(u8 data)
//{
//	static _dt_rx_anl_st rx_anl;

//	
//	switch (rx_anl.rxstate)
//	{
//		case 0:
//		{
//			if(data==0xAA)
//			{
//				rx_anl.rxstate=1;
//				rx_anl.DT_RxBuffer[0]=data;
//			}
//		}
//		break;
//		case 1:
//		{
//			if((data == 0xFF || data == 0xFA || data == 0x05))
//			{
//				rx_anl.rxstate=2;
//				rx_anl.DT_RxBuffer[1]=data;
//			}
//			else
//			{
//				rx_anl.rxstate = 0;
//			}			
//		}
//		break;
//		case 2:
//		{
//			rx_anl.rxstate = 3;
//			rx_anl.DT_RxBuffer[2]=data;	
//		}
//		break;
//		case 3:
//		{
//			if(data<(DT_RX_BUFNUM-5))
//			{
//				rx_anl.rxstate = 4;
//				rx_anl.DT_RxBuffer[3]=data;
//				rx_anl._data_len = data;
//				rx_anl._data_cnt = 0;
//			}
//			else
//			{
//				rx_anl.rxstate = 0;
//			}			
//		}
//		break;
//		case 4:
//		{
//			if(rx_anl._data_len>0)
//			{
//				rx_anl._data_len--;
//				rx_anl.DT_RxBuffer[4+rx_anl._data_cnt++]=data;
//				if(rx_anl._data_len==0)
//				{
//					rx_anl.rxstate = 5;
//				}
//			}
//			else
//			{
//				rx_anl.rxstate = 0;
//			}
//		}
//		break;
//		case 5:
//		{
//			rx_anl.rxstate = 6;
//			rx_anl.DT_RxBuffer[4+rx_anl._data_cnt++]=data;		
//		}
//		break;		
//		case 6:
//		{
//			u8 _data_cnt;
//			rx_anl.rxstate = 0;
//			rx_anl.DT_RxBuffer[4+rx_anl._data_cnt]=data;
//			_data_cnt = rx_anl._data_cnt+5;
//			//ano_dt_data_ok = 1;
//			AnoDTDataAnl(rx_anl.DT_RxBuffer, _data_cnt);		
//		}
//		break;	
//		default :
//		{
//			rx_anl.rxstate = 0;
//		}break;
//	}
//}
//uint8_t Buffer_fly[20]={0};
//uint8_t Rx_fly;
//uint8_t i = 0;
//uint8_t A_location,B_location; 
//uint8_t Send_flag = 0;
//uint8_t recv_state=0;
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
//	if(huart == &huart3) 
//	{
//		AnoDTRxOneByte(Rx_fly);

//		HAL_UART_Receive_IT(&huart3,&Rx_fly,1);
//	}
//	
//}
//void Datasend8_t(uint8_t data_r)
//{
//	HAL_UART_Transmit(&huart3,&data_r, 1, 1);
//}
//uint8_t Fly_protocol[11] = {0xAA, 0x05, 0xE0, 0x05, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
//void FlyDataSend(uint8_t *data_s,uint8_t Aim)
//{
//	Fly_protocol[6] = Aim;
//	Fly_protocol[7] = 0xA0+00;
//	Fly_protocol[8] = 0xB0+00;
//	Data_check(Fly_protocol,9);
//	Fly_protocol[9] = sum_check;
//	Fly_protocol[10] = add_check;
//	for(uint16_t j = 0; j < 11; j++)
//	{
//		Datasend8_t(Fly_protocol[j]);
//	}
//}
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
//	HAL_UART_Receive_IT(&huart3,&Rx_fly,1);
  /* USER CODE END 2 */
		entrance();
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
